﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Jocker;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.Domain.ProductionWorkOrders;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using System.Collections.Generic;
using System.Runtime.Intrinsics.Arm;

namespace MangoMes.Write.API.Applications.Pickinges
{
    /// <summary>
    /// 生产领料列表
    /// </summary>
    public class PickingSeachHandler : IRequestHandler<PickingSeachCommand, PageDTO<PickingsDto>>
    {
        #region 仓储
        /// <summary>
        /// 生产领料仓储
        /// </summary>
        private readonly IBateRepository<PickingsDto> PickingRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<PickingSeachHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="pickingRepository"></param>
        /// <param name="logger"></param>
        public PickingSeachHandler(IBateRepository<PickingsDto> pickingRepository, ILogger<PickingSeachHandler> logger)
        {
            PickingRepository = pickingRepository;
            this.logger = logger;
        }

        #endregion

        #region 生产领料列表 
        /// <summary>
        /// 生产领料 列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PageDTO<PickingsDto>> Handle(PickingSeachCommand request, CancellationToken cancellationToken)
        {

            //异常处理
            try
            {
                //初始化一个ResultDTO<PickingsDto>对象result，并设置Code为 0
                PageDTO<PickingsDto> result = new PageDTO<PickingsDto>();

                var pageNum = (request.pageIndex - 1) * request.pageSize;
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper dapperHelper = new DapperHelper();
                PickingsDto dto = new PickingsDto();

                //连接mySql 
                string conn = dapperHelper.MangoMesWrite;

                //sql语句
                string str = $"select * from picking a join productionworkorders b on a.ProductionWorkOrdersNo = b.ProductionWorkOrdersNo join workstation c on a.WorkStations = c.WorkStations join clientmanagement d on a.ClientNo= d.ClientNo";

                var list = PickingRepository.GetAll(str, dto, conn).ToList();

                if (!string.IsNullOrEmpty(request.pickingNode))
                {
                    list = list.Where(x => x.PickingNode.Contains(request.pickingNode)).ToList();
                }
                if (!string.IsNullOrEmpty(request.pickingName))
                {
                    list = list.Where(x => x.PickingName.Contains(request.pickingName)).ToList();
                }
                if (request.documents > 0)
                {
                    list = list.Where(x => x.Documents == request.documents).ToList();
                }
                if (request.pickingTime != null)
                {
                    list = list.Where(x => x.PickingTime == request.pickingTime).ToList();
                }

                result.TotalCount = list.Count(); //计算list列表中元素个数
                result.PageCount = (int)Math.Ceiling((list.Count() * 1.0) / request.pageSize); //表示总的记录数量
                list = list.OrderBy(x => x.PickingId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList(); //计算总页数
                result.Data = list;

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
